export function urlQuery(name) {
  let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
  let r = window.location.search.substr(1).match(reg)
  if (r != null) {
    return unescape(r[2])
  }
  return null
}

export function qsStringify(data) {
  let ret = ''
  for (let it in data) {
    ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
  }
  return ret.slice(0, -1)
}

export function StrQuery(urlStr, key) {
  if (urlStr) {
    let obj = {}
    let arr1 = urlStr.split('&')
    if (arr1.length) {
      for (let i = 0; i < arr1.length; i++) {
        let arr2 = arr1[i].split('=')
        obj[arr2[0]] = arr2[1]
      }
    }
    if (key) {
      return obj[key]
    }
    return obj
  }
}

export const clipboard = {
  set: (txt) => {
    if (document.queryCommandSupported('copy')) {
      let textarea = document.createElement("textarea")
      textarea.value = txt
      textarea.readOnly = "readOnly"
      document.body.appendChild(textarea)
      textarea.select()
      textarea.setSelectionRange(0, txt.length)
      document.execCommand("copy")
      if (!!window.ActiveXobject || "ActiveXObject" in window) { // 判断是否是IE
        textarea.removeNode(true)
      } else {
        textarea.remove()
      }
    }
  },
  get: () => {
    if (navigator.clipboard) {
      return navigator.clipboard.readText()
    } else {
      if (window.clipboardData && window.clipboardData.getData) { // IE
        var pastedText = window.clipboardData.getData('Text');
        // pastedText = e.originalEvent.clipboardData.getData('Text');//e.clipboardData.getData('text/plain');
        return Promise.resolve(pastedText)
      }
      return Promise.reject('no')
    }
  }
}